<?php
/**
 * Created by PhpStorm.
 * User: Gooraye
 * Date: 2016-08-03
 * Time: 10:37
 */

namespace Home\Controller;
use Shop\Api\GroupattendApi;
use Shop\Api\ProductApi;
use Shop\Api\ProductGroupApi;
use Shop\Api\ProductSkuApi;
use Think\Controller;
use Shop\Api\OrdersApi;
use Shop\Api\GroupApi;
use Shop\Api\OrdersItemApi;
use Weixin\Api\WxuserApi;
class TasktimeController extends Controller{

    public function index(){
        addWeixinLog('定时器启动');
        $this->checkinfo();
        $this->confirm_express();
        $this->automatic_refund();
        $this->close_group();
    }

    public function checkinfo(){
        //过期订单处理将过期订单的商品重新上架
        //找到有未付款的订单
        $model = M();
        $model->startTrans();//开启事务
        $flag = false;

        $distancenow = time() - 3600;
        $ordermap['pay_status'] = 0;
        $ordermap['status'] = 1;
        $ordermap['o.createtime'] = array('LT',$distancenow);
        //连表查询找到相应的商品id
        $productidinfo = apiCall(OrdersApi::SELECT_SELL_ORDER,array($ordermap,'i.p_id,i.count'));
        if($productidinfo['status']){
            unset($ordermap['o.createtime']);
            $entity['status'] = -1;
            $ordermap['createtime'] = array('LT',$distancenow);
            $orderinfo = apiCall(OrdersApi::SAVE,array($ordermap,$entity));
            if($orderinfo['status']){
                foreach($productidinfo['info'] as $v){
                    $skumap['product_id'] = $v['p_id'];
                    $skuinfo = apiCall(ProductSkuApi::GET_INFO,array($skumap));
                    if($skuinfo['status']){
                        $skusave['quantity'] = $skuinfo['info']['quantity']+$v['count'];
                        $skures = apiCall(ProductSkuApi::SAVE,array($skumap,$skusave));
                        if(!$skures['status']){
                            $flag = true;
                        }
                    }
                }
               //还有一种状态没写对于已经卖完的商品重新上架
            }else{
                $flag = true;
            }
        }
        if ($flag) {
            $model->rollback();
        } else {
            addWeixinLog('定时器-订单重新上架');
            $model->commit();
        }

    }

    //15天后自动确认收货+将购买商品的钱转到卖家余额上(15*24*3600)
    public function confirm_express(){
        $model = M();
        $model->startTrans();//开启事务
        $flag = false;
        $ordermap['order_status'] = 4;
        $distancenow = time()-(15*24*3600);
        $ordermap['updatetime'] = array('LT',$distancenow);
        //找到所有该类型的订单
        $ordercode = apiCall(OrdersApi::QUERY_NO_PAGING,array($ordermap,'','order_code'));
        if($ordercode['status']){
            $ordercodearr = array_column($ordercode['info'],'order_code');
            foreach($ordercodearr as $vo){
                //找到卖家信息存入钱
                $result = apiCall(OrdersItemApi::ORDEPX, array(array('itboye_orders_item.order_code' => $vo), false, 'itboye_orders_item.order_code,itboye_orders_item.price,itboye_orders_item.post_price,itboye_wxuser.id,itboye_wxuser.money'));
                $wxusermap['id'] = $result['info']['id'];
                $moneysave['money'] = bcadd($result['info']['money'],bcadd($result['info']['price'] , $result['info']['post_price'],2),2);
                $accountres = apiCall(WxuserApi::SAVE, array($wxusermap, $moneysave));
                if (!$accountres['status']) {
                    $flag = true;
                }
            }
            $entity['order_status'] = 5;//表示确认收货交易成功
            $orderinfo = apiCall(OrdersApi::SAVE,array($ordermap,$entity));
            if(!$orderinfo['status']){
                $flag = true;
            }
        }

        if ($flag) {
            $model->rollback();
        } else {
            addWeixinLog('定时器-自动确认收货');
            $model->commit();
        }
    }

    //申请退款10天后自动将钱退到买家余额上(10*24*3600)
    public function automatic_refund(){
        $distancenow = time()-(10*24*3600);
        $model = M();
        $model->startTrans();//开启事务
        $flag = false;
        $map['create_time'] = array('LT',$distancenow);
        $map['result'] = 2;
        $entity['result'] = 1;
        $ordersrefundmodal =  M('OrdersRefundMoney');
        //找到退款的订单号
        $selectarr = $ordersrefundmodal -> where($map) -> select();
        if($selectarr){
            $idstr = array_column($selectarr,'order_code');

            foreach($idstr as $oc){
                //找到买家信息存入钱
                $result = apiCall(OrdersApi::ORDER_WXUSER,array(array('o.order_code' => $oc),'o.price,o.post_price,w.id,w.money'));
                if($result['status']){
                    $wxusermap['id'] = $result['info']['id'];

                    $moneysave['money'] = bcadd($result['info']['money'],bcadd($result['info']['price'] , $result['info']['post_price'],2),2);

                    $accountres = apiCall(WxuserApi::SAVE, array($wxusermap, $moneysave));

                    if (!$accountres['status']) {
                        $flag = true;
                    }
                }else{
                    $flag = true;
                }
            }

        }else{
            $flag = true;
        }
        if(!$flag){
            $result = $ordersrefundmodal -> create($entity, 2);

            if($result === false){
                $flag = true;
            }else{
                $result = $ordersrefundmodal-> where($map) -> save();
                if(!$result){
                    $flag = true;
                }
            }
        }
        if ($flag) {
            $model->rollback();
        } else {
            addWeixinLog('定时器-自动将钱退到买家余额上');
            $model->commit();
        }
    }


    //过了时间自动将团关闭
    public function close_group(){
        $model = M();
        $model->startTrans();//开启事务
        $flag = false;

        $distancenow = time();
        $ordermap['end_time'] = array('LT',$distancenow);
        //找到所有该类型的团购
        $ordercode = apiCall(ProductGroupApi::CLOSE_GROUP,array($ordermap,''));
        if($ordercode['status']){
            if(!empty($ordercode['info'])){
                $orderpidarr=$ordercode['info'];
                foreach($orderpidarr as $vo){
                    //找到相对应的团购商品
                    $map=[];$map['id']=$vo['p_id'];$entity['status']=-2;
                    $result=apiCall(ProductApi::SAVE,array($map,$entity));
                    if(!$result['status']){
                        $flag=true;
                    }
                    //找到对应的团
                    $map=[];$map['productid']=$vo['p_id'];$map['status']=1;$entity['status']=-1;
                    $group=apiCall(GroupApi::QUERY_NO_PAGING,array($map));
                    if($group['status']&&!empty($group['info'])){


                        foreach($group['info'] as $k=>$v){

                            if($v['status']==1){
                                //关闭团
                                $ma=[];$ma['id']=$v['id'];
                                $result=apiCall(GroupApi::SAVE,array($ma,$entity));
                                //退还团成员的金额
                                $people['groupid']=$v['id'];
                                $customer=apiCall(GroupattendApi::QUERY_NO_PAGING,array($people));
                                if($customer['status']&&!empty($customer['info'])){
                                    foreach($customer['info'] as $kk =>$vv){
                                        $ma['id']=$vv['uid'];
                                        $money_original=apiCall(WxuserApi::GET_INFO,array($ma));
                                        $money_original=$money_original['info']['money'];
                                        $m['order_code']=$vv['ordercode'];
                                        $money=apiCall(OrdersApi::GET_INFO,array($m));
                                        if(!$money['status']) $flag=true;
                                        $money=$money['info']['price'];
                                        $entity['money']=$money_original+$money;$map=[];$map['id']=$vv['uid'];
                                        $moneyback=apiCall(WxuserApi::SAVE,array($map,$entity));
                                        if(!$moneyback['status']) $flag=true;
                                    }
                                }
                                if(!$result['status']){
                                    $flag=true;
                                }}}}
                }
            }else{
                $flag=true;
            }
        }
        if ($flag) {
            $model->rollback();
        } else {
            addWeixinLog('定时器-自动下架过期团购商品');
            $model->commit();
        }
    }



}